#include "mt3d_cuda.h"

void mt3d::read_config(std::string filename)
{
    std::clog << "Reading configuration file ... ";
    std::ifstream infile;
    gctl::open_infile(infile, filename, ".config");

    int is_modeling;
    double frequency;
    infile >> tetgen_file_ >> is_modeling >> frequency >> theta_in_ 
        >> station_marker_ >> bound_marker_ >> air_marker_;

    if (is_modeling) modeling_only_ = true;
    else modeling_only_ = false;

    omega_ = 2.0*pi*frequency;

    // Set primary model
    int tmp_size;
    infile >> tmp_size;
    if (tmp_size < 2)
    {
        throw gctl::runtime_error("Invalid primary model sizes: "+std::to_string(tmp_size));
    }

    // Set primary model
    primary_model_.resize(tmp_size);
    for (int i = 0; i < tmp_size; ++i)
    {
        infile >> primary_model_[i].cndt >> primary_model_[i].mu 
            >> primary_model_[i].epsl >> primary_model_[i].btm_depth;
    }

    le_.set_layers(primary_model_);
    le_.set_frequency(frequency);
    le_.initialize();

    // Get secondary model
    infile >> tmp_size;
    if (tmp_size <= 0)
    {
        throw gctl::runtime_error("Invalid secondary model sizes: "+std::to_string(tmp_size));
    }

    secondary_model_.resize(tmp_size);
    for (int i = 0; i < tmp_size; ++i)
    {
        infile >> secondary_model_[i].tag >> secondary_model_[i].cndt >> secondary_model_[i].ref_cndt 
            >> secondary_model_[i].mu >> secondary_model_[i].epsl;
    }

    infile.close();
    std::clog << "done\n";
    return;
}